import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/my",
    name: "my",
    component: () => import("../views/My.vue"),
  },
  {
    path: "/detail",
    name: "detail",
    component: () => import("../views/Detail.vue"),
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),
  },
  {
    path: "/index",
    name: "index",
    component: () => import("../views/Index.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("../views/Home.vue"),
      },
      {
        path: "/order",
        name: "order",
        component: () => import("../views/Order.vue"),
      },
      {
        path: "/shopcar",
        name: "shopcar",
        component: () => import("../views/Shopcar.vue"),
        beforeEnter(to, from, next) {
          if (localStorage.getItem("username")) {
            next();
          } else {
            next("/login");
          }
        },
        //单个路由规则独享的守卫 跟组件级守卫功能一样 但是建议用组件级守卫
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

// router.beforeEach((to, from, next) => {
//   // console.log("全局守卫");
//   // next(); //放行路由
//   if (localStorage.getItem("username")) {
//     next();
//     //如果本地存储中有 username证明已经登陆了 就执行下一步
//   } else {
//     if (to.path == "/login") {
//       next();
//     } else {
//       next("/login");
//     }
//     //为了防止死循环 如果是要跳转login 就放行 就不会死循环了，如果不是login就跳转到login
//   }
// });
//路由跳转之前执行

export default router;
